package com.ruoyi.web.controller.tool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.webContent.domain.PlTeam;
import com.ruoyi.webContent.service.IPlTeamService;

import java.util.Date;
import java.util.List;

public class PlTeamMakeUtils {

    public static void setTeam(SysDictData gameDict){
        IPlTeamService plTeamService = (IPlTeamService) ApplicationContextUtil.getBean("plTeamServiceImpl");
        String gameValue = gameDict.getDictValue();
        String gameName = gameDict.getDictLabel();

        //雷电竞
        for (Object o:PlDataSetUtils.getHostgameAllMatch("ldj",gameName)) {
            JSONObject json = JSONObject.parseObject(o.toString());
            JSONArray team = json.getJSONArray("team");
//            Map<String, String> l_teamMap = new HashMap<>();
            //雷电竞队伍信息存储（以yb为主）
            for (Object team_l : team) {
                JSONObject team_json = JSONObject.parseObject(team_l.toString());
                PlTeam plTeam = new PlTeam();
                plTeam.setGameName(gameValue);
                String ldjId = team_json.getString("team_id").trim();
                plTeam.setLdjId(ldjId);
                List<PlTeam> plTeams = plTeamService.selectPlTeamList(plTeam);
                String team_short_name = team_json.getString("team_short_name").trim();
                plTeam.setTeamName(team_short_name);
                if(plTeams.size()<1){
                    plTeam.setLdjId(ldjId);
                    plTeam.setStartTime(json.getString("start_time"));
                    plTeamService.insertPlTeam(plTeam);
                }else{
                    for (PlTeam p:plTeams) {
                        p.setTeamName(team_short_name);
                        p.setStartTime(json.getString("start_time"));
                        plTeamService.updatePlTeam(p);
                    }
                }
            }
        }
        //亚博
        for (Object o:PlDataSetUtils.getHostgameAllMatch("yb",gameName)) {
            JSONObject json = JSONObject.parseObject(o.toString());
            //PHTName
            PlTeam plTeam = new PlTeam();
            plTeam.setGameName(gameValue);
            String PHTId = json.getString("PHTId").trim();
            String startTime=json.getString("PMCDate");
            startTime=startTime.replace("T"," ");
            Date date = DateUtils.addHours(DateUtils.parseDate(startTime.substring(0, 19)), 12);
            startTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date);
            plTeam.setYbId(PHTId);
            List<PlTeam> plTeams = plTeamService.selectPlTeamList(plTeam);
            if(plTeams.size()<1){
                PlTeam plTeam_new = new PlTeam();
                plTeam_new.setGameName(gameValue);
                plTeam_new.setYbId(PHTId);
                plTeam_new.setTeamSortName(json.getString("PHTName").trim());
                plTeam_new.setStartTime(startTime);
                plTeamService.insertPlTeam(plTeam_new);
            }else{
                for (PlTeam p:plTeams) {
                    p.setTeamSortName(json.getString("PHTName").trim());
                    p.setStartTime(startTime);
                    plTeamService.updatePlTeam(p);
                }
            }
            //PATName
            PlTeam plTeam2 = new PlTeam();
            plTeam2.setGameName(gameValue);
            String PATId = json.getString("PATId").trim();
            plTeam2.setYbId(PATId);
            List<PlTeam> plTeams2 = plTeamService.selectPlTeamList(plTeam2);
            if(plTeams2.size()<1){
                PlTeam plTeam_new = new PlTeam();
                plTeam_new.setGameName(gameValue);
                plTeam_new.setYbId(json.getString("PATId").trim());
                plTeam_new.setTeamSortName(json.getString("PATName").trim());
                plTeam_new.setStartTime(startTime);
                plTeamService.insertPlTeam(plTeam_new);
            }else{
                for (PlTeam p:plTeams2) {
                    p.setTeamSortName(json.getString("PATName").trim());
                    p.setStartTime(startTime);
                    plTeamService.updatePlTeam(p);
                }
            }

        }
        //雷火
        for (Object o:PlDataSetUtils.getHostgameAllMatch("lh",gameName)) {
            JSONObject json = JSONObject.parseObject(o.toString());
            JSONObject home = json.getJSONObject("home");
            JSONObject away = json.getJSONObject("away");
            PlTeam plTeam = new PlTeam();
            plTeam.setGameName(gameValue);
            String homeId = home.getString("team_id").trim();
            plTeam.setLhId(homeId);
            List<PlTeam> homes = plTeamService.selectPlTeamList(plTeam);
            String homeTeamName = home.getString("team_name").trim();
            if(homes.size()==0){
                plTeam.setStartTime(json.getString("start_datetime").trim());
                plTeam.setTeamLhName(homeTeamName);
                plTeamService.insertPlTeam(plTeam);
            }else{
                for (PlTeam p:homes) {
                    p.setTeamLhName(homeTeamName);
                    p.setStartTime(json.getString("start_datetime").trim());
                    plTeamService.updatePlTeam(p);
                }
            }
            //away
            PlTeam plTeam2 = new PlTeam();
            plTeam2.setGameName(gameValue);
            String awayId = away.getString("team_id").trim();
            plTeam2.setLhId(awayId);
            List<PlTeam> aways = plTeamService.selectPlTeamList(plTeam2);
            String awayTeamName = away.getString("team_name").trim();
            if(aways.size()<1){
                plTeam2.setStartTime(json.getString("start_datetime").trim());
                plTeam2.setTeamLhName(awayTeamName);
                plTeamService.insertPlTeam(plTeam2);
            }else{
                for (PlTeam p:aways) {
                    p.setTeamLhName(awayTeamName);
                    p.setStartTime(json.getString("start_datetime").trim());
                    plTeamService.updatePlTeam(p);
                }
            }
        }
        //数据库队伍去重
        //取出亚博不为空雷电竞为空的队伍
        List<PlTeam> yb_ldj = plTeamService.selectTeamByType("yb_ldj");
        //取出雷电竞不为空亚博为空的队伍
        List<PlTeam> ldj_yb = plTeamService.selectTeamByType("ldj_yb");
        //双层循环找出名字一样的
        for (PlTeam yb :yb_ldj) {
            for (PlTeam ldj :ldj_yb) {
                //名字一样的删除雷电竞数据，补全亚博数据
                if(yb.getTeamSortName().toLowerCase().replace(" ", "")
                        .equals(ldj.getTeamName().toLowerCase().replace(" ", ""))){
                    plTeamService.deletePlTeamById(ldj.getId());
                    yb.setTeamName(ldj.getTeamName());
                    yb.setLdjId(ldj.getLdjId());
                    yb.setTeamLhName(ldj.getTeamLhName());
                    yb.setLhId(ldj.getLhId());
                    plTeamService.updatePlTeam(yb);
                }
            }
        }
        //取出亚博不为空雷火为空的队伍
        List<PlTeam> yb_lh = plTeamService.selectTeamByType("yb_lh");
        //取出雷火不为空亚博为空的队伍
        List<PlTeam> lh_yb = plTeamService.selectTeamByType("lh_yb");
        //双层循环找出名字一样的
        for (PlTeam yb :yb_lh) {
            for (PlTeam lh :lh_yb) {
                //名字一样的删除雷火数据，补全亚博数据
                if(yb.getTeamSortName().toLowerCase().replace(" ", "")
                        .equals(lh.getTeamLhName().toLowerCase().replace(" ", ""))){
                    plTeamService.deletePlTeamById(lh.getId());
                    yb.setTeamLhName(lh.getTeamLhName());
                    yb.setLhId(lh.getLhId());
                    yb.setTeamName(lh.getTeamName());
                    yb.setLdjId(lh.getLdjId());
                    plTeamService.updatePlTeam(yb);
                }
            }
        }
        //数据库队伍去重
        //取出雷电竞不为空雷火为空的队伍
        List<PlTeam> ldj_lh = plTeamService.selectTeamByType("ldj_lh");
        //取出雷火不为空雷电竞为空的队伍
        List<PlTeam> lh_ldj = plTeamService.selectTeamByType("lh_ldj");
        //双层循环找出名字一样的
        for (PlTeam lh :lh_ldj) {
            for (PlTeam ldj :ldj_lh) {
                //名字一样的删除雷电竞数据，补全雷火数据
                if(lh.getTeamLhName().toLowerCase().replace(" ", "")
                        .equals(ldj.getTeamName().toLowerCase().replace(" ", ""))){
                    plTeamService.deletePlTeamById(ldj.getId());
                    lh.setTeamName(ldj.getTeamName());
                    lh.setLdjId(ldj.getLdjId());
                    lh.setTeamSortName(ldj.getTeamSortName());
                    lh.setYbId(ldj.getYbId());
                    plTeamService.updatePlTeam(lh);
                }
            }
        }

        //删除超过当前时间的队伍信息
        plTeamService.deletePlTeamBefore();
    }
}
